package com.lvchanglong

import com.grailslm.User
import org.hibernate.SessionFactory
import org.hibernate.cfg.Configuration

/**
 * Example Groovy class.
 */
class Example {

    static Configuration cfg = null
    static SessionFactory sessionFactory = null

    static {
        cfg = new Configuration().configure() //读取配置文件hibernate.cfg.xml
        sessionFactory = cfg.buildSessionFactory() //创建SessionFactory过程中，会创建数据库表，也可认为创建了数据库连接池
        println "初始化完成，连接池已开启" //领会精神
    }

    /**
     * 增
     */
    static def save() {
        def session = sessionFactory.openSession() //从连接池中获取连接
        def transaction =  session.beginTransaction() //开启事务

        def robotA = new Robot([name:"机器人A"]) //没有id，因为id是自增的
        robotA.mouths.add(new Mouth([name:"大嘴"]))
        session.save(robotA)

        def robotB = new Robot([name:"机器人B"]) //没有id，因为id是自增的
        robotB.mouths.add(new Mouth([name:"中嘴"]))
        robotB.mouths.add(new Mouth([name:"小嘴"]))
        session.save(robotB)

        def robotC = new Robot([name:"机器人C"]) //没有id，因为id是自增的
        robotC.mouths.add(new Mouth([name:"香肠嘴"]))
        session.save(robotC)

        transaction.commit() //提交事务
        session.close() //让连接重返连接池
    }

    /**
     * 读
     */
    static def show() {
        def session = sessionFactory.openSession()
        def robot = session.get(Robot, 2)
        println "-----------------------------------------------------------------------show：" + robot + "##" + robot.mouths
        session.close()
    }

    /**
     * 改
     */
    static def update() {
        def session = sessionFactory.openSession() //从连接池中获取连接
        def transaction =  session.beginTransaction() //开启事务

        def robot = session.get(Robot, 2)
        robot.mouths[0].name = "匿名嘴"
        session.update(robot)

        transaction.commit() //提交事务
        session.close() //让连接重返连接池
    }

    /**
     * 删
     */
    static def delete() {
        def session = sessionFactory.openSession() //从连接池中获取连接
        def transaction =  session.beginTransaction() //开启事务

        def robot = session.get(Robot, 2)
        session.delete(robot)

        transaction.commit() //提交事务
        session.close() //让连接重返连接池
    }

    /**
     * 列表
     */
    static def list() {
        def session = sessionFactory.openSession()
        def hqlQuery = session.createQuery("from Robot")
        println "-----------------------------------------------------------------------list：" + hqlQuery.list()

        def sqlQuery = session.createSQLQuery("SELECT * FROM robot")
        sqlQuery.addEntity(Robot) //将list结果转化为对象
        println "-----------------------------------------------------------------------list：" + sqlQuery.list()

        session.close()
    }


    static main(args) {
        try {
//            this.save()

//            this.show()
//            this.update()
//            this.show()

//            this.delete()
//            this.list()
        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            println "演示完毕，关闭连接池" //领会精神
            this.sessionFactory.close()
        }
    }

}
